//
//  CZUserAccount.swift
//  SinaBlog
//
//  Created by 李超 on 16/3/22.
//  Copyright © 2016年 lichao. All rights reserved.
//

import UIKit

class CZUserAccount: NSObject,NSCoding {
    
    var expires_Date:NSDate?
    var access_token:String?
    var avatar_large:String?
    var screen_name:String?
    //基本数据类型
    var expires_in: NSTimeInterval = 0{
        //设置信息过时日期
        didSet{
            expires_Date = NSDate(timeIntervalSinceNow: expires_in)
        }        
    }

    var uid:String?
    //重写构造方法
    init(dict:[String : AnyObject]) {
        super.init()
        //通过KVC进行赋值
        setValuesForKeysWithDictionary(dict)
    }
    //重写该方法，避免在键值对不匹配是造成崩溃
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
    }
    //重写打印对象的方法
    override var description: String {
        let keys = ["access_token","expires_in","uid","screen_name","avatar_large"]
        return dictionaryWithValuesForKeys(keys).description
    }
    //实现归档的方法
    //归档
    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(expires_Date, forKey: "expires_Date")
        aCoder.encodeObject(access_token, forKey: "access_token")
        aCoder.encodeDouble(expires_in, forKey: "expires_in")
        aCoder.encodeObject(uid, forKey: "uid")
        aCoder.encodeObject(screen_name, forKey: "screen_name")
        aCoder.encodeObject(avatar_large, forKey: "avatar_large")
    }    
    //解挡
    required init?(coder aDecoder: NSCoder) {
        expires_in = aDecoder.decodeDoubleForKey("expires_in")
        access_token = aDecoder.decodeObjectForKey("access_token") as? String
        uid = aDecoder.decodeObjectForKey("uid") as? String
        expires_Date = aDecoder.decodeObjectForKey("expires_Date") as? NSDate
        screen_name = aDecoder.decodeObjectForKey("screen_name") as? String
        avatar_large = aDecoder.decodeObjectForKey("avatar_large") as? String
    }
    
    
}
